package dmsxl.erchashu;

/**
 * Author: Zhang Dongwei
 * Date: 2023/5/20 15:29
 * 验证二叉搜索树
 */
public class ecs23_98 {

    public static void main(String[] args) {
        TreeNode node1 = new TreeNode(5);
        TreeNode node2 = new TreeNode(1);
        TreeNode node3 = new TreeNode(4);
        TreeNode node4 = new TreeNode(3);
        TreeNode node5 = new TreeNode(6);
        node1.left = node2;
        node1.right = node3;
        node3.left = node4;
        node4.left = node5;
        System.out.println(isValidBST(node1));
    }

    static TreeNode pre = new TreeNode();
    public static boolean isValidBST(TreeNode root) {
        System.out.println(pre.val);
        if (root == null) return true;
        boolean left = isValidBST(root.left);
        if (pre != null && pre.val >= root.val){
            return false;
        }
        pre = root;
        boolean right = isValidBST(root.right);
        return left && right;
    }
}
